// pages/vedio/vedio.js
let num;
let number=0;
let number2=0;
let flag=true;
let flag2=true;
let arr2=new Array(),arrB=new Array(),collect2=new Array()
const app=getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      author:'',
      time:'',
      finger:'',
      url:'',
      clickCollect:'',
      id:'',
      collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c30.png'
    },

    click:function(){

    collect2=wx.getStorageSync('collect2')
    console.log(collect2)
    number=0
    var that=this
    num=parseInt(Math.random()*arrB.length)  

    that.setData({
      author:arrB[num].video_author,
      time:arrB[num].video_time,
      url:arrB[num].video_url,
      id:arrB[num].video_id
    })
    for(let i=0;i<collect2.length;i++){
      if(that.data.id==collect2[i].video_id){
        number=number+1
        console.log(collect2[i])
        that.setData({
          clickCollect:'取消收藏',
          collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c18.png'
        })
        
      }
    }
    if(number==0){
  
      that.setData({
        clickCollect:'点击收藏',
        collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c30.png'
      })
    } 
    },
  collect:function(){
    collect2=wx.getStorageSync('collect2')
    
    //先判断
    //flag为true是收藏
    number2=0
    // console.log(arrA)
    var that=this
    //是红心
    for(let i=0;i<collect2.length;i++){
      if(that.data.id==collect2[i].video_id){
        number2++
        flag=true
        console.log(that.data.id)
        console.log(collect2[i])

      }
    }
    console.log(number2)
    if(number2==0){
      wx.request({
        url: 'https://wx.xxmtcds.top:2333/user/collectvideo',
        method:'POST',
        name:'collectList2',
        data:{
          id:that.data.id
        },
        header:{
          'Content-Type': 'application/json',
            authorization:app.globalData.token
        },
        success:function(res){
          console.log(res.data)
          console.log(res)
          wx.request({
            url: 'https://wx.xxmtcds.top:2333/user/collectvideo',
            method:'GET',
            name:'collectList2',
            header:{
              'Content-Type': 'application/json',
                authorization:app.globalData.token
            },
            success:function(res){
              console.log(res)
              wx.setStorageSync('collect2', res.data)
              that.setData({
                clickCollect:'取消收藏',
                collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c18.png'
              })
            }
          })
        }
      })
    }
    if(number2!=0){
      wx.request({
        url: 'https://wx.xxmtcds.top:2333/user/collectvideo',
        method:'POST',
        name:'collectList2',
        data:{
          id:that.data.id
        },
        header:{
          'Content-Type': 'application/json',
            authorization:app.globalData.token
        },
        success:function(res){
          console.log(res)
          wx.request({
            url: 'https://wx.xxmtcds.top:2333/user/collectvideo',
            method:'GET',
            name:'collectList2',
            header:{
            'Content-Type': 'application/json',
            authorization:app.globalData.token
      
          },
          success:function(res){
            console.log(res)
            that.setData({
              clickCollect:'点击收藏',
              collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c30.png'
            })
            wx.setStorageSync('collect2', res.data)
          }
          })
        }
      })
    }

   
  },
  onShow:function(){
    arr2=wx.getStorageSync('arr2')
    collect2=wx.getStorageSync('collect2')
    arrB=wx.getStorageSync('arrB')
    var that=this

    that.setData({
          author:arr2.author,
          time:arr2.time,
          url:arr2.url,
          id:arr2.id
    })
    //判断collect里面是不是有当前这个arr的id
    for(let i=0;i<collect2.length;i++){
      if(collect2[i].video_id==arr2.id){
        // console.log(collect[i])
        that.setData({
          clickCollect:'取消收藏',
          collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c18.png'
        })
      }
    }
    
    let style=wx.getStorageSync('style')
    if(style!=''){
      this.setData({
        contentStyle:wx.getStorageSync('style')
      })
    }else{
      this.setData({
        contentStyle:'https://pic.imgdb.cn/item/6199ad6c2ab3f51d91e20c91.jpg'
      })
    }
    let children=wx.getStorageSync('children')
      if(children!==''){
        this.setData({
            children:wx.getStorageSync('children')
        })
      }else{
        this.setData({
            children:'https://pic.imgdb.cn/item/6199ab9b2ab3f51d91e171d6.png'
        })
      }
  }
  
  })